<?php
/**
 * common model file Created by PhpStorm.
 * User: yang
 * Date: 2020/04/28
 */

namespace Common\Model;

class Power extends Base
{
    protected $table = 'power';
    protected $guarded = [];

    //public $timestamps = false;

    //一对一关联管理员
    public function get_admin()
    {
        return $this->hasOne(Admin::class, 'id', 'admin_id')
            ->select('id', 'name');
    }

    //一对多关联自己
    public function get_power()
    {
        return $this->hasMany(Power::class, 'pid', 'id')
            ->select('id', 'pid', 'name');
    }

    //一对多关联角色
    public function get_roles_power()
    {
        return $this->hasMany(Power::class, 'pid', 'id')
            ->select('id', 'pid', 'name');
    }


    /**
     * 读取数据处理
     * @param $value
     * @return string
     */
    public function getTypeAttribute($value)
    {
        switch ($value) {
            case 1:
                $result = '菜单';
                break;
            case 2:
                $result = '按钮';
                break;
            default:
                $result = '未知';
        }
        return $result;
    }
}